package com.demo.webapp.ui.table;

import javax.servlet.ServletContext;

import com.demo.webapp.ui.RepositoryBuilderBase;
import com.demo.webapp.ui.config.ComponentRulesModule;
import com.demo.webapp.ui.displayer.DisplayerMapping;

public class TableRepositoryBuilder extends RepositoryBuilderBase {

	public static final String[] REPOSITORY_TABLE_BASENAME = {"classpath:com/demo/webapp/ui/table/digester-table.xml"};
	
	private TableRepositoryResource tableRepository;
	
	/**
	 * 初始化MenuRepositoryBuilder数据
	 */
	private void init() {
		tableRepository = new TableRepositoryResource();
		//使用默认配置文件位置
		if (getBaseNames() == null || getBaseNames().length ==0) {
			setBaseNames(TableRepositoryBuilder.REPOSITORY_TABLE_BASENAME);
		}
	}
	
	@Override
	public void beforeLoad(ComponentRulesModule module) {

		init();
		//设置栈顶对象,XML文件将操作这个对象将属性注入
		push(this.tableRepository);
		if (log.isDebugEnabled()) {
			log.debug(module);
		}
		
	}

	@Override
	public void afterLoad(ServletContext servletContext) {
		log.debug("after load.");
		DisplayerMapping mapping = this.tableRepository.getMapping();
		if (mapping == null) {
			if (log.isDebugEnabled()) {
				log.debug("DisplayerMapping is null.");
			}
		}
        //将持久对象TableRepositoryResource放入Servlet容器上下文中
		servletContext.setAttribute(TableRepositoryResource.TABLE_REPOSITORY_KEY, pop());
	}

}
